package org.lc.design.samples.case2.threadsafe;

import java.util.HashMap;
import java.util.Map;

/**
* 缓存管理器（线程安全-双重检查机制实现）
* @author lc
* @date 2024/7/8
*/
public class SafeCacheManager {

    private static SafeCacheManager instance;

    private Map<String, Object> cache;

    private SafeCacheManager() {
        cache = new HashMap<>();
    }

    public static SafeCacheManager getInstance() {
        if(instance == null) {
            synchronized (SafeCacheManager.class) {
                if(instance == null) {
                    instance = new SafeCacheManager();
                }
            }
        }
        return instance;
    }

    public void set(String key, Object value) {
        cache.put(key, value);
    }

    public Object get(String key) {
        return cache.get(key);
    }

    public void remove(String key) {
        cache.remove(key);
    }
}
